#include "StdAfx.h"
#include "HubImage.h"
#include "DDLoadFileList.h"



DDLoadFileList::DDLoadFileList(void)
{
	memset(m_szFileDir,0,sizeof(m_szFileDir));

	m_CBFun     = NULL;
	m_dwContext = 0;
	m_pContext  = NULL;
}

DDLoadFileList::~DDLoadFileList(void)
{
	DDThread::DDThdUnInit();
}

int DDLoadFileList::SetLoadFileListCBFun(OnLoadFileListCBFun fun,DWORD dwContext,void *pContext)
{
	m_CBFun     = fun;
	m_dwContext = dwContext;
	m_pContext  = pContext;

	return 0;
}

int DDLoadFileList::SetFileDir(char *pFilrDir)
{
	DDThread::DDThdUnInit();

	Safesprintf(m_szFileDir,_T("%s"),pFilrDir);

	if( !DDThread::DDThdInit() )
	{
		return -1;
	}

	return 0;
}

int DDLoadFileList::DDThdDispatch(HANDLE hExitEvent)
{
	if( NULL == m_CBFun )
	{
	   return -1;
	}

	char szFileDir[MAX_PATH];
	char szFileName[MAX_PATH];
	char  szFileName2[MAX_PATH];

	zkImgFileTail xFileTail;

	Safesprintf(szFileDir,_T("%s\\*.*"),m_szFileDir);

	WIN32_FIND_DATA   filedata; 
	HANDLE            filehandle; 

	filehandle = FindFirstFile( szFileDir,&filedata); 

	if(filehandle!=INVALID_HANDLE_VALUE) 
	{ 
		do 
		{ 
			if( FILE_ATTRIBUTE_DIRECTORY != filedata.dwFileAttributes )
			{
				if( strlen( filedata.cFileName ) > 4 ) 
				{ 
					Safesprintf(szFileName,_T("%s\\%s"),m_szFileDir,filedata.cFileName);

					WideChar2MultiByte(szFileName, szFileName2);

					memset(&xFileTail,0,sizeof(xFileTail));
					if( 0 == DD_ImagePro::GetFileInfo(szFileName2,&xFileTail) )
					{
                        if( xFileTail.CheckVersion() )
						{
                            m_CBFun(filedata.cFileName,&xFileTail,m_dwContext,m_pContext);
						}
					}
				} 
			}
			
		}while( DDThread::m_bThreadRun  && FindNextFile(filehandle,&filedata) ); 

		FindClose(filehandle); 
	} 


	return 0;
}

